package website.digitalocean.chapter15;

/**
 * @author: longhc@citycloud.com.cn
 * @since: 2023/2/15 15:05
 * @version: 1.0
 */
public class ObserverPatternTest {

    public static void main(String[] args) {
        // 定义事件发布者
        MyTopic myTopic = new MyTopic();

        // 定义事件监听者并指定监听者名称
        Observer observer1 = new MyTopicSubscriber("observer1");
        Observer observer2 = new MyTopicSubscriber("observer2");
        Observer observer3 = new MyTopicSubscriber("observer3");

        // 建立发布者与监听者的关联关系
        myTopic.register(observer1);
        myTopic.register(observer2);
        myTopic.register(observer3);

        //
        observer1.setSubject(myTopic);
        observer2.setSubject(myTopic);
        observer3.setSubject(myTopic);

        observer1.update();

        myTopic.postMessage("good");
    }
}
